<template>
    <div class="padding20">
        <!--面包屑导航区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>


        <!--卡片视图-->
        <el-card>
            <!--添加角色按钮-->
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
                </el-col>
            </el-row>

            <!--表格-->
            <tree-table
                class="marginTop20" 
                :data="cateList"
                :columns="columns"
                :selection-type="false"
                :expand-type="false"
                show-index
                index-text="#"
                border
                >
                <!--是否有效-->
                <template slot="isok" slot-scope="scoped">
                    <i class="el-icon-success" style="color:lightgreen" v-if="!scoped.row.cat_delete"></i>
                    <i class="el-icon-error" style="color:lightgreen" v-else></i>
                </template>
                <!--排序-->
                <template slot="order" slot-scope="scoped">
                    <el-tag size="mini" v-if="scoped.row.cat_level===0">一级</el-tag>
                    <el-tag type="success" size="mini" v-if="scoped.row.cat_level===1">二级</el-tag>
                    <el-tag type="warning" size="mini" v-if="scoped.row.cat_level===2">三级</el-tag>
                </template>
                <!--操作-->
                <template slot="opt" slot-scope="scoped">
                    <el-button type="primary" size="mini" icon="el-icon-edit" @click="editCate(scoped.row)">编辑</el-button>
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="DelCate(scoped.row)">删除</el-button>
                </template>
            </tree-table>

            <!--分页组件区域-->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5,10,20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="cateTotal">
            </el-pagination>
        </el-card>

        <!--添加分类对话框-->
        <el-dialog
            title="添加分类"
            :visible.sync="showdialogVisible"
            width="50%"
            @close="closeAddCateDialog">
            <!--内容主体区-->
            <el-form :model="addCateFrom" :rules="addCateRules" ref="addCateFromRef" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateFrom.cat_name"></el-input>
                </el-form-item>
                <!--级联选择器 s
                    options  制定渲染数据
                    props  制定配置对象
                -->
                <el-form-item label="父级分类">
                    <el-cascader
                    v-model="selectKeys"
                    :options="parentCateList"
                    :props="slectedCateProps"
                    @change="handleChange"
                    clearable
                    >
                    </el-cascader>
                </el-form-item>
            </el-form>
            <!--底部按钮区-->
            <span slot="footer">
                <el-button @click="closeAddCateDialog">取 消</el-button>
                <el-button type="primary" @click="handleDialogPost">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import {handleGetCates,handleAddCate} from '@/request/cate'
export default {
    props: {

    },
    components: {

    },
    data() {
        return {
            //请求列表参数
            queryInfo:{
                type: 3,
                pagenum: 1,
                pagesize: 5
            },
            //总条数
            cateTotal: 0,
            //列表数据
            cateList:[],
            //为tree-table指定列
            columns:[
                {
                    label:'分类名称',
                    prop: 'cat_name'
                },
                {
                    label:'是否有效',
                    // 将当前列定义为模板列
                    type:'template',
                    // 当前列使用模板名称
                    template:'isok'
                },
                {
                    label:'排序',
                    type:'template',
                    template:'order'
                },
                {
                    label:'操作',
                    type:'template',
                    template:'opt'
                }
            ],
            showdialogVisible:false,
            addCateFrom:{
                cat_name: '',
                cat_pid: 0,  // 分类父级id 默认0
                cat_level: 0 //分类等级 一0  二1  三2
            },
            addCateRules:{
                cat_name:[
                    { required: true, message: '请输入分类名', trigger: 'blur' },
                    { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
                ],
            },
            parentCateList:[],    //级联渲染数据
            slectedCateProps:{    //级联选择器配置对象
                value: 'cat_id',
                label: 'cat_name',
                children: 'children',
                checkStrictly: true   //允许任意选中一二级
            },
            selectKeys:[]         //选中的数组对象
        };
    },
    computed: {

    },
    created() {
        this.getCateListData()
    },
    mounted() {

    },
    watch: {

    },
    methods: {
        /*
            级联选择器选项被改变
        */
       handleChange(){
           if(this.selectKeys.length==0){//没选东西
                this.addCateFrom.cate_pid = 0
                this.addCateFrom.cate_level = 0
           } else{  //有
                this.addCateFrom.cate_pid = this.selectKeys[this.selectKeys.length-1]  //取最后那个即可
                this.addCateFrom.cate_level =  this.selectKeys.length==1? 1 : 2    //数组只有一个值代表选了一个一级
           } 
        },
        /*
                确定添加分类
        */
        handleDialogPost(){
            this.$refs.addCateFromRef.validate(async valid=>{
                if(valid){  //校验通过
                    handleAddCate(this.addCateFrom)
                    .then(res=>{
                        if(res.meta.status!==201){
                            return this.$message.error('添加分类失败')
                        }else{
                            this.showdialogVisible = false
                            this.getCateListData()
                            this.$message.success(res.meta.msg)
                        }
                    })
                }else{
                  this.$message.error('请完善分类信息')
                }
            })
        },
        /*
            获取列表数据
        */
        async getCateListData(){
            let res =  await handleGetCates(this.queryInfo)
            if(res.meta.status!==200){
                return this.$message.error('获取商品分类列表失败')
            }else{
               this.cateList = res.data.result
               this.cateTotal = res.data.total
            }
        },
                /*
            监听pageSize改变
        */
        handleSizeChange(newSize){
            this.queryInfo.pagesize = newSize
            this.getCateListData(this.queryInfo)
        },
        /*
            监听页码值page改变
        */
        handleCurrentChange(newPage){
            this.queryInfo.pagenum = newPage
            this.getCateListData(this.queryInfo)
        },
        /*
            打开添加分类dialog
        */
        async showAddCateDialog(){
            this.showdialogVisible = true
            let params = { type: 2}
            let res = await handleGetCates(params)
            if(res.meta.status!==200){
                return this.$message.error('获取父级分类列表失败')
            }else{
               this.parentCateList = res.data
            }
        },
        closeAddCateDialog(){
            this.showdialogVisible = false
            //数据和校验清空
            this.addCateFrom = {
                cate_name: '',
                cate_pid: 0,  
                cate_level: 0 
            }
            this.selectKeys = []
            this.$refs.addCateFromRef.resetFields()
        }
    },
};
</script>

<style scoped lang="less">
.el-card{
    width: 1200px;
}

</style>
